new Vue({
    el:"#app",
    data:{
        userId:1,
        comment:"",
        currentTime:"",
        commentList:[],
    },
    methods:{
    sendComment() {
    this.currentTime = this.getFormatDate();
    console.log(this.userId+this.comment+this.currentTime);
    if (this.comment == '') {
        alert("请先输入评论内容");
        return;
    }
    let li = $("<li style=\"width: 100%;\"></li>");
    let p1 = $("<p class=\"Cuser\" style=\"width: 100px;margin-left: 460px;\"> <label>用户:</label> <span style=\"color: #FF00FF\">" + this.userId + "</span></p>");
    let p2 = $("<p class=\"Ccontent\" style=\"width: 580px;margin-left: 460px;\"><label>所评:</label><span>" + this.comment + "</span></p>");
    let p3 = $("<p class=\"Ctime\" style=\"width: 200px;margin-left: 460px;\"> <span style=\"font-size: 12px;color:gray\">" + this.currentTime + "</span></p>");
    let hr = $("<hr  style=\"margin-top:20px\">");
    li.append(p1);
    li.append(p2);
    li.append(p3);
    li.append(hr);
    axios.get("/comments/insertDoc", {
        params: {
            user: this.userId,
            content:this.comment,
            date: this.currentTime,
        }
    }).then(response => {
        if (response.data > 0) {
            console.log("成功"+response.data);
            $("#commentList").append(li);
            this.comment="";
            this.userId++;
        } else {
            console.log("失败"+response.data);
        }
    }).catch(function (error) {
        alert(error);
    })
},
   getFormatDate(){
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
    var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
    var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
},
},
mounted(){
    axios.get("/comments/selectDoc").then(response => {
        var obj =  response.data;
        if(obj.length>0){
            for (let i = 0; i <obj.length ; i++) {
                this.commentList.push(obj[i]);
            }
        }
    }).catch(function(error){
        alert(error);
    })
    },

})


